﻿using FRMS.BUS;
using FRMS.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System;

namespace FRMS.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    //[Authorize]
    public class RecordController : ControllerBase
    {
        private JwtSettings _jwtSettings;
        IHttpContextAccessor _access;
        public RecordController(IOptions<JwtSettings> jwtSettingsAccesser, IHttpContextAccessor access
          )
        {
            JwtSettings jwt = new JwtSettings();
            jwt.Audience = AppSettingsHelper.Configuration["JwtSettings:Audience"];
            jwt.Issuer = AppSettingsHelper.Configuration["JwtSettings:Issuer"];
            jwt.SecretKey = AppSettingsHelper.Configuration["JwtSettings:SecretKey"];
            _jwtSettings = jwt;
            _access = access;
        }

        /// <summary>
        /// 查询设备记录
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryDeviceRecord")]
        public ResponseMessage QueryDeviceRecord(DeviceRecord deviceRecord)
        {
            ResponseMessage message = new ResponseMessage();
            RecordBUS BUS = new RecordBUS();
            try
            {
                deviceRecord.projectID = Convert.ToInt32(_access.HttpContext.Session.GetString("ProjectID"));
                message = BUS.QueryDeviceRecordList(deviceRecord);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "查询设备记录";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "查询设备记录异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 删除设备记录
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("DeleteDeviceRecord")]
        public ResponseMessage DeleteDeviceRecord(DeviceRecord deviceRecord)
        {
            ResponseMessage message = new ResponseMessage();
            RecordBUS BUS = new RecordBUS();
            try
            {
                message = BUS.DeleteDeviceRecord(deviceRecord.ID);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "删除设备记录";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "删除设备记录异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 查询乘梯记录
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryLadderRecord")]
        public ResponseMessage QueryLadderRecord(LadderRecord ladderRecord)
        {
            ResponseMessage message = new ResponseMessage();
            RecordBUS BUS = new RecordBUS();
            try
            {
                message = BUS.QueryLadderRecoedList(ladderRecord);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "查询乘梯记录";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "查询乘梯记录异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 删除乘梯记录
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("DeleteLadderRecord")]
        public ResponseMessage DeleteLadderRecord(LadderRecord ladderRecord)
        {
            ResponseMessage message = new ResponseMessage();
            RecordBUS BUS = new RecordBUS();
            try
            {
                message = BUS.DeleteLadderRecord(ladderRecord.ID);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "删除设备记录";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "删除设备记录异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 查询切换模式记录
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryWorkModeStateRecord")]
        public ResponseMessage QueryWorkModeStateRecord(WorkModeStateRecord workModeState)
        {
            ResponseMessage message = new ResponseMessage();
            RecordBUS BUS = new RecordBUS();
            try
            {
                workModeState.projectID = Convert.ToInt32(_access.HttpContext.Session.GetString("ProjectID"));
                message = BUS.QueryWorkModeStateRecordList(workModeState);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "查询切换模式记录";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "查询切换模式记录异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 查询切换模式记录
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryControllerStateRecord")]
        public ResponseMessage QueryControllerStateRecord(DeviceControllerStateRecord deviceControllerState)
        {
            ResponseMessage message = new ResponseMessage();
            RecordBUS BUS = new RecordBUS();
            try
            {
                deviceControllerState.projectID = Convert.ToInt32(_access.HttpContext.Session.GetString("ProjectID"));
                message = BUS.QueryControllerStateRecordList(deviceControllerState);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "查询控制器记录";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "查询控制器记录异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 新增梯控返回结果记录
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("InsertLadderRecord")]
        public ResponseMessage InsertLadderRecord(string Device_UUID,string Owner_UUID)
        {
            ResponseMessage message = new ResponseMessage();
            RecordBUS BUS = new RecordBUS();
            try
            {
                message = BUS.InsertLadder(Device_UUID, Owner_UUID);
                if (message.errcode==0)
                {
                    message = BUS.DeleteLadder(Device_UUID);
                }
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "新增梯控返回结果";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "新增梯控返回结果异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

    }
}
